import { Controller, UseGuards } from '@nestjs/common';
import { Crud, CrudAuth,CrudController, Override, ParsedRequest, ParsedBody, CrudRequest } from '@nestjsx/crud';
import { ApiTags, ApiBearerAuth, ApiBody} from '@nestjs/swagger';
import { Order } from '../../entities/order.entities';
import { OrderService } from '../../services/order/order.service';
import { AuthGuard } from '@nestjs/passport';
import * as moment from 'moment';
import { OrderGoodsService } from '../../services/order-goods/order-goods.service';
import { In } from 'typeorm';
import { OrderGoods } from '../../entities/order_goods.entities';
import { ProductService } from '../../services/product/product.service';

@Crud({
  model:{
    type:Order,
  },
  query:{
    exclude:["user_id","shipping_id","coupon_id","parent_id"],
    join:{
      orderGoods:{
        exclude:['goods_id','order_id','product_id']
      },
      'orderGoods.product':{
        exclude:[]
      },
      'orderGoods.product.goods':{
        exclude:["category_id","brand_id","keywords","is_on_sale","is_delete","attribute_category","is_new","app_exclusive_price","is_app_exclusive","is_limited","is_hot","create_user_id","update_user_id","create_user_dept_id","promotion_tag","add_time","update_time","counter_price","extra_price","unit_price"]
      },
    }
  },
  routes:{
    only:['createOneBase','deleteOneBase','getManyBase']
  }
})
@CrudAuth({
  property:"user",
  filter: (user) => ({
    user_id: user.userId
  }),
  persist:(user) => {
    return user;
  }
})
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiTags('order')
@Controller('api/order')
export class OrderController {
  constructor(
    public service:OrderService,
    private productService:ProductService
  ){}

  get base(): CrudController<Order> {
    return this;
  }

  @ApiBody({
    type: Order
  })
  @Override()
  async createOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: any): Promise<Order> {
    dto.user_id=req.parsed.authPersist.userId
    const tradeNo = 'alipay_'+moment().format('YYYYMMDDHHmmss');
    dto.order_sn=tradeNo;
    dto.add_time=moment().toISOString();
    const product_ids=[];
    for(let item of dto.orderGoods){
      product_ids.push(item.product_id);  
    }
    //console.log(product_ids);
    const products=await this.productService.repo.find({
      where:{
        id:In(product_ids)
      },
      relations:['goods']
    });
    //console.log(products);
    for(let product of products){
      const orderGoods=dto.orderGoods.filter(item => item.product_id==product.id)[0];
      orderGoods.goods_id=product.goods_id;
      orderGoods.goods_sn=product.goods_sn;
      orderGoods.goods_name=product.goods.name;
      orderGoods.goods_specifition_ids=product.goods_specification_ids;
      orderGoods.product_id=product.id;
      orderGoods.list_pic_url=product.goods.list_pic_url;
      orderGoods.market_price=product.goods.market_price;
      orderGoods.retail_price=product.goods.retail_price;
      //console.log(orderGoods);
    }
    return this.base.createOneBase(req, dto);
  }

  @ApiBody({
    type: Order
  })
  @Override()
  updateOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: Order): Promise<Order> {
    dto.user_id=req.parsed.authPersist.userId
    return this.base.updateOneBase(req, dto);
  }
}
